//
// Ephi - simulation of magnetic fields and particles
// Copyright (C) 2007 Indrek Mandre <indrek(at)mare.ee>
// For more information please see http://www.mare.ee/indrek/ephi/
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "ephi.hpp"

#define WIDTH 800
#define HEIGHT 800

#define COILMATRIX_SIDE 13

int main (int argc, char *argv[])
{
  prec_t radius = 0.15;
  prec_t wr = 0.05;
  prec_t spacing = 0.01;
  prec_t I = 134560;

  prec_t fmin, fmax;
  fmin = fmax = 0;

  ShapedCoilFactory round_shaper(COILMATRIX_SIDE);
  round_shaper.set_circle();

  Statics statics;
  ElectroDynamics dyn(statics);
  make_polywell_cube (statics, radius, wr, spacing, I, 0, round_shaper);
  Scene scene(dyn);
  Screen screen(WIDTH, HEIGHT, 0.85, 0.85*HEIGHT/WIDTH);

  Screen axes(WIDTH, HEIGHT, 1.0, 1.0);
  axes.set_axis (vect3d(0, 0.0, 0), vect3d(1, 0, 0), vect3d(0, 1, 0));
  axes.draw_axes();
  statics.draw (axes, Screen::BLACK);
  axes.write ("polywell_slice_round_axes.bmp");

  screen.set_axis (vect3d(0, 0, 0.1), vect3d(1, 0, 0), vect3d(0, 1, 0));
  scene.calc (screen);
  // we calibrate off the center a bit so not to get too big of a range
  // due to field erasure in the middle
  fmin = scene.get_fmin();
  fmax = scene.get_fmax();

  prec_t offset = 0;
  while ( offset <= radius * 3 )
    {
      screen.set_axis (vect3d(0, 0, offset), vect3d(1, 0, 0), vect3d(0, 1, 0));
      scene.calc (screen);
      scene.calibrate_field (fmin, fmax);
      scene.set_coloring (32, true);
      scene.render_map (screen);
      char path[128];
      sprintf (path, "polywell_slice_round_loglevel_%.5f.bmp", prec2double(offset));
      screen.write (path);

      scene.set_coloring (0, true);
      scene.render_map (screen);
      sprintf (path, "polywell_slice_round_log_%.5f.bmp", prec2double(offset));
      screen.write (path);
      offset += 0.005;
    }

  return 0;
}

